package com.study.jyg.rxjava2study.utils;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;

import androidx.annotation.RequiresApi;
import androidx.core.graphics.drawable.DrawableCompat;

import java.io.InputStream;

/**
 * 图片工具类
 */
public class IconUtils {


    /**
     * 修改图片颜色
     *
     * @param mBitmap
     * @param mColor
     * @return
     */
    public static Bitmap getAlphaBitmap(Bitmap mBitmap, int mColor) {
//          BitmapDrawable mBitmapDrawable = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.enemy_infantry_ninja);
//          Bitmap mBitmap = mBitmapDrawable.getBitmap();

        //BitmapDrawable的getIntrinsicWidth（）方法，Bitmap的getWidth（）方法
        //注意这两个方法的区别
        //Bitmap mAlphaBitmap = Bitmap.createBitmap(mBitmapDrawable.getIntrinsicWidth(), mBitmapDrawable.getIntrinsicHeight(), Config.ARGB_8888);
        Bitmap mAlphaBitmap = Bitmap.createBitmap(mBitmap.getWidth(), mBitmap.getHeight(), Bitmap.Config.ARGB_8888);

        Canvas mCanvas = new Canvas(mAlphaBitmap);
        Paint mPaint = new Paint();

        mPaint.setColor(mColor);
        //从原位图中提取只包含alpha的位图
        Bitmap alphaBitmap = mBitmap.extractAlpha();
        //在画布上（mAlphaBitmap）绘制alpha位图
        mCanvas.drawBitmap(alphaBitmap, 0, 0, mPaint);

        return mAlphaBitmap;
    }

    /**
     * 修改图片颜色
     * @param drawable
     * @return
     */
    @RequiresApi(api = Build.VERSION_CODES.O)
    public static Drawable tintDrawable(Drawable drawable, float alpha, float red, float green, float blue) {
        Drawable wrappedDrawable = DrawableCompat.wrap(drawable);
        DrawableCompat.setTintList(wrappedDrawable, ColorStateList.valueOf(Color.argb(alpha, red, green, blue)));
        return wrappedDrawable;
    }

    /**
     * 修改图片颜色
     * @param r
     * @param g
     * @param b
     * @param a
     */
    public static float[] getIconColor( int r, int g, int b, float a) {
        return new float[]{
                0, 0, 0, 0, r,
                0, 0, 0, 0, g,
                0, 0, 0, 0, b,
                0, 0, 0, (float) a / 255, 0
        };
    }


    /**
     * 资源转bitmap 5个方法
     * @param mContext
     * @param rid
     * @return
     */
    public static Bitmap FromRidToBitmap1(Context mContext, int rid) {
        return BitmapFactory.decodeResource(mContext.getResources(), rid, null);
    }

    public static Bitmap FromRidToBitmap2(Context mContext, int rid) {
        Drawable drawable = mContext.getResources().getDrawable(rid);
        BitmapDrawable bd = (BitmapDrawable) drawable;
        return bd.getBitmap();
    }

    public static Bitmap FromRidToBitmap3(Context mContext, int rid) {
        InputStream is = mContext.getResources().openRawResource(rid);
        return BitmapFactory.decodeStream(is);
    }

    public static Bitmap FromRidToBitmap4(Context mContext, int rid) {
        Bitmap bmp = BitmapFactory.decodeResource(mContext.getResources(), rid);
        return Bitmap.createBitmap(300, 300, Bitmap.Config.ARGB_8888);
    }

    public static Bitmap FromRidToBitmap5(Context mContext, int rid) {
        Resources r = mContext.getResources();
        InputStream is = r.openRawResource(rid);
        BitmapDrawable bmpDraw = new BitmapDrawable(is);
        return bmpDraw.getBitmap();
    }
}
